package com.doeis.manage.service;

import java.util.ArrayList;
import java.util.List;

import com.doeis.core.db.DBManager;
import com.doeis.manage.bean.User;
import com.doeis.manage.dao.UserDao;
import com.doeis.tools.DateHelper;

public class UserService {
	private UserDao dao = new UserDao();

	/**
	 * 根据姓名查用户
	 * 
	 * @param username
	 * @return
	 * @throws Exception
	 * @author wxy 2016年3月19日星期六中国标准时间下午2:52:17
	 */
	public User findByName(String username) throws Exception {
		try {
			String sql = "select * from " + User.tableName + " where username=? ";
			List<Object> params = new ArrayList<Object>();
			params.add(username);
			return dao.read(User.class, sql, params.toArray());
		} catch (Exception e) {
			throw e;
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public boolean insertUser(User user) throws Exception {
		try {
			return dao.insert(user);
		} catch (Exception e) {
			throw e;
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public boolean update(User user) throws Exception {
		try {
			return dao.update(user);
		} catch (Exception e) {
			throw e;
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public User findByCookie(String value) throws Exception {
		try {
			String sql = "select * from " + User.tableName + " where cookie_token=? and cookie_token_expire>?";
			List<Object> params = new ArrayList<Object>();
			params.add(value);
			params.add(DateHelper.currentDateTime());
			return dao.read(User.class, sql, params.toArray());
		} catch (Exception e) {
			throw e;
		}finally{
			DBManager.closeAllBindConnection();
		}
	}
}
